import QR from "@/common/qrcode.js"
export default {
	data() {
		return {
			imagePath: '',
		}
	},
	onLoad() {},
	methods: {
		//适配不同屏幕大小的canvas
		setCanvasSize() {
			let size = {};
			try {
				let res = uni.getSystemInfoSync();
				let scale = 750 / 686; //不同屏幕下canvas的适配比例；设计稿是750宽
				let width = res.windowWidth / scale;
				let height = width; //canvas画布为正方形
				size.w = width;
				size.h = height;
			} catch (e) {
				console.log("获取设备信息失败" + e);
			}
			return size;
		},
		// 绘制图片
		createQrCode(url, canvasId, cavW, cavH) {
			QR.api.draw(url, canvasId, cavW, cavH, this, this.canvasToTempImage);
		},
		//获取临时缓存照片路径，存入data中
		canvasToTempImage() {
			uni.canvasToTempFilePath({
				canvasId: 'mycanvas',
				success: (res)=> {
					var tempFilePath = res.tempFilePath;
					this.imagePath = tempFilePath
					console.log(tempFilePath)
				},
				fail: function(res) {
					console.log(res);
				}
			}, this);
		},
		//点击图片进行预览，长按保存分享图片
		previewImg(e) {
			uni.showModal({
				title: '提示',
				content: '是否将保存到相册吗?',
				success: (e) => {
					if (e.confirm) {
						uni.saveImageToPhotosAlbum({
							filePath: this.imagePath,
							success: () => {
								uni.showToast({
									title: "保存成功",
									icon: "none"
								});
							},
							fail: (err) => {
								uni.showToast({
									title: "保存失败",
									icon: "none"
								});
							}
						});
						// uni.downloadFile({
						// 	url:"https://img0.baidu.com/it/u=3437217665,1564280326&fm=26&fmt=auto", //图片地址
						// 	// filePath:wx.env.USER_DATA_PATH + '/' + this.imagePath,
						// 	success: (res) => {
						// 		if (res.statusCode === 200) {
						// 			console.log(res.tempFilePath)
						// 			uni.saveImageToPhotosAlbum({
						// 				filePath: res.tempFilePath,
						// 				success: () => {
						// 					uni.showToast({
						// 						title: "保存成功",
						// 						icon: "none"
						// 					});
						// 				},
						// 				fail: (err) => {
						// 					uni.showToast({
						// 						title: "保存失败",
						// 						icon: "none"
						// 					});
						// 				}
						// 			});
						// 		}
						// 	},
						// 	fail: (err) => {
						// 		console.log(err)
						// 		uni.showToast({
						// 			title: "保存失败",
						// 			icon: "none"
						// 		});
						// 	}
						// })
					}
				}
			})
		},
	}
}
